<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Repository\WxNotifyRepository;
use App\Repository\WxPayRepotitory;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;

class WxPayController extends Controller
{
    public $wxPay;
    /**
     * WxPayController constructor.
     */
    public function __construct()
    {
        $this->middleware('exclusivescope')->only('getPreOrder');
    }

    /**
     * 获取预订单信息【向微信服务器发送预订单请求】
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function getPreOrder()
    {
        $id = app('request')->input('id');

        $rules = ['id' => ['required', 'min:0', 'numeric', 'integer']];

        $validator = app('validator')->make(['id'=>$id], $rules);
        if ($validator->fails()){
            throw new SuspiciousOperationException('id字段输入错误', null, null, [], 422);
        }

        $wxPay = new WxPayRepotitory($id);
        return $wxPay->pay();
    }

    /**
     * 微信回调方法
     */
    public function receiveNotify()
    {
        //1.检查库存量
        //2.修改订单status状态
        //3.减库存
        //如果成功处理，想微信返回成功信息，否则像微信服务器发送失败信息
        //微信返回参数特点：1，Post  2，xml格式参数 3，回调路由不能用?携带参数
        $wxNotify = new WxNotifyRepository();
        $wxNotify->handle();

    }
}



